package com.jeecg.p3.system.order.service;

import com.jeecg.p3.base.util.DateUtil;
import com.jeecg.p3.system.order.dao.ProductDetailsDao;
import com.jeecg.p3.system.order.entity.ProductDetails;
import com.jeecg.p3.system.poster.dao.CustomerCouponDao;
import com.jeecg.p3.system.poster.entity.CustomerCoupon;
import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.PageQueryWrapper;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *
 * @ClassName: CourseOrderService
 * @Description: TODO 订单详情处理层
 * @author: lzx
 * @date: 2020年12月01日 下午7:29:38
 */
@Service
public class ProductDetailsService {

    private static final Logger log = LoggerFactory.getLogger(ProductDetailsService.class);

    @Resource
    private ProductDetailsDao dao;
    @Resource
    private CustomerCouponDao customerCouponDao;

    /**
     * 分页查询订单管理信息
     * @param pageQuery
     * @return
     */
    public PageList<ProductDetails> queryPageList(PageQuery<ProductDetails> pageQuery) {
        PageList<ProductDetails> result = new PageList<ProductDetails>();
        Integer itemCount = dao.count(pageQuery);
        PageQueryWrapper<ProductDetails> wrapper = new PageQueryWrapper<ProductDetails>(pageQuery.getPageNo(), pageQuery.getPageSize(),itemCount, pageQuery.getQuery());
        List<ProductDetails> list = dao.queryPageList(wrapper);
        Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
        result.setPagenation(pagenation);
        result.setValues(list);
        return result;
    }

    /**
     * 新增支付订单管理信息
     * @param productDetails
     */
    public void doAdd(ProductDetails productDetails) {
        productDetails.setCreateDate(DateUtil.dateToString(new Date()));
        productDetails.setCreateTime(DateUtil.dateToString(new Date(), "HH:mm:ss"));
        dao.insert(productDetails);
    }

    /**
     * 新增手动开通订单信息
     * @param productDetails
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertPd(ProductDetails productDetails) {
        dao.insert(productDetails);
        String cusCouponId = productDetails.getCouponId();
        CustomerCoupon customerCoupon = customerCouponDao.get(cusCouponId);
        customerCoupon.setUpdateDate(productDetails.getCreateDate());
        customerCoupon.setUpdateTime(productDetails.getCreateTime());
        customerCoupon.setUpdateUser(productDetails.getCreateUser());
        customerCoupon.setOpenType("2");
        customerCoupon.setUseStatus("1");
        customerCouponDao.update(customerCoupon);
    }

    /**
     * 根据参数获取订单管理信息数量
     * @param productDetails
     * @return
     */
    public Integer count(ProductDetails productDetails) {
        PageQuery<ProductDetails> pageQuery = new PageQuery<>();
        pageQuery.setQuery(productDetails);
        return dao.count(pageQuery);
    }

    /**
     * 根据id查询订单详情管理信息1
     * @param id
     * @return
     */
    public ProductDetails findById(Long id) {
        return dao.get(id);
    }

    /**
     * 根据传参获取订单详情管理列表信息
     * @param courseOrder
     * @return
     */
    public List<ProductDetails> findByParam(ProductDetails courseOrder) {
        return dao.findByParam(courseOrder);
    }

    /**
     * 去重查询用户已购买的课程信息
     * @param productDetails
     * @return
     */
    public List<ProductDetails> findByParamGroupBy(ProductDetails productDetails) {
        return dao.findByParamGroupBy(productDetails);
    }


    /**
     * 根据订单号查询订单信息
     * @param posterNo
     * @return
     */
    public ProductDetails findByOpenId(String posterNo) {
        return dao.findByOpenId(posterNo);
    }
    /**
     * 编辑订单管理信息
     * @param courseOrder
     */
    public void doEdit(ProductDetails courseOrder) {
        dao.update(courseOrder);
    }

    /**
     * 删除订单管理信息
     * @param courseOrder
     */
    public void doDelete(ProductDetails courseOrder) {
        courseOrder.setStatus("0");
        dao.update(courseOrder);
    }
}
